package decision_tree.question;

import java.util.List;

import decision_tree.dto.Sample;


public class NgramQuestion implements Question {
	List<String> ngram;
	
	@Override
	public void initialize(List<String> terms) {
		ngram = terms;
	}
	
	@Override
	public int ask(Sample sample) {
		List<String> x = sample.getX();
		if(x.size() < ngram.size()) {
			return 0;
		}
		
		boolean flag = true;
		int offset = x.size() - ngram.size();
		for(int i = 0; i < ngram.size(); i++) {
			String key = getString(x.get(offset + i));
			String key2 = ngram.get(i);
			
			if(equal(key, key2)) {
				flag = false;
				break;
			}
		}
		
		return flag ? 1 : 0;
	}

	@Override
	public String toString() {
		return "ngram :  " + ngram.toString();
	}
	
	public String getString(String key) {
		return key;
	}
	
	protected boolean equal(String a, String b) {
		return a.compareTo(b) == 0;
	}
}
